package com.example.web03homework.service.impl;

import com.example.web03homework.dao.CategoryDao;
import com.example.web03homework.exception.ServiceException;
import com.example.web03homework.pojo.Category;
import com.example.web03homework.pojo.extend.CategoryExtend;
import com.example.web03homework.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public Category getById(Integer id) {
        return categoryDao.getById(id);
    }

    @Override
    public void addCategory(Category category) {
        categoryDao.insert(category);
    }

    @Override
    public void modifyCategory(Category category) {
        categoryDao.update(category);
    }

    @Override
    public void removeById(Integer id) throws Exception {
        // 获取要删除的分类信息
        Category category = categoryDao.getById(id);
        if (category == null) {
            throw new ServiceException("分类不存在");
        }

        // 判断是否为一级分类还是二级分类
        if (category.getParentId() == null || category.getParentId() == 0) {
            // 一级分类
            // 检查该一级分类及其子分类下是否有书籍
            int bookCount = categoryDao.countBooksByCategoryIdWithChildren(id);
            if (bookCount > 0) {
                throw new ServiceException("该分类或其子分类下存在书籍，无法删除");
            }

            // 先删除所有二级分类
            categoryDao.deleteByParentId(id);

            // 再删除一级分类
            categoryDao.deleteById(id);
        } else {
            // 二级分类
            // 检查该分类下是否有书籍
            int bookCount = categoryDao.countBooksByCategoryId(id);
            if (bookCount > 0) {
                throw new ServiceException("该分类下存在书籍，无法删除");
            }

            // 直接删除该二级分类
            categoryDao.deleteById(id);
        }
    }

    public List<Category> getAllCategories() {
        return categoryDao.getAllCategories();
    }

    @Override
    public List<CategoryExtend> getAllCategoriesWithChildren() {
        // 获取所有一级分类
        List<Category> primaryCategories = categoryDao.getPrimaryCategories();
        
        // 转换为CategoryExtend并获取每个一级分类的子分类
        return primaryCategories.stream().map(primaryCategory -> {
            CategoryExtend categoryExtend = new CategoryExtend();
            categoryExtend.setId(primaryCategory.getId());
            categoryExtend.setName(primaryCategory.getName());
            categoryExtend.setDescription(primaryCategory.getDescription());
            categoryExtend.setParentId(primaryCategory.getParentId());
            
            // 获取该一级分类下的所有二级分类
            List<Category> children = categoryDao.getChildrenByParentId(primaryCategory.getId());
            categoryExtend.setChildren(children);
            
            return categoryExtend;
        }).collect(Collectors.toList());
    }
}
